<?php

Yii::import('common.models.db.ShopCategoryModel');

class FrontendShopCategoryModel extends ShopCategoryModel
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	public static function getCategory($parentId=0)
	{
		$sql = "SELECT * FROM shop_category WHERE published=1 AND parent_id=:pid ORDER BY position ASC";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':pid', $parentId, PDO::PARAM_INT);
		return $command->queryAll();
	}
	public function hasSubCategory($catId)
	{
		$sql = "SELECT count(*) as cnt from shop_category WHERE published=1 AND parent_id=:cid";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':cid', $catId, PDO::PARAM_INT);
		$result = $command->queryScalar();
		if($result>0) return true;
		return false;
	}
	public function getAllCategory()
	{
		$criteria = new CDbCriteria;
		$criteria->condition = 'published=1';
		$criteria->order = 'position ASC';
		return self::model()->findAll($criteria);
	}
	public static function getAllCategoryToRoot($childCatId, &$allCat=array())
	{
		$allCat[]  = $childCatId;
		
		$parentCat = self::model()->findByPk($childCatId);
		$parentId  = $parentCat->parent_id;
		if($parentId>0){
			self::getAllCategoryToRoot($parentId, $allCat);
		}
	}
}